package com.ysg.widget.dialog.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.PagerAdapter;
import com.ysg.R;
import com.ysg.utils.YCollectionUtil;
import com.ysg.widget.dialog.entity.GiftListResult;
import java.util.List;

public class GiftPageAdapter extends PagerAdapter {
    private Context context;

    private List<List<GiftListResult.GiftBean>> publicGifItemBean;

    public GiftPageAdapter(Context context) {
        this.context = context;
    }

    /**
     * 决定了有多少页
     */
    @Override
    public int getCount() {
        return publicGifItemBean.size();
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        return POSITION_NONE;
    }

    public void setMaps(List<List<GiftListResult.GiftBean>> maps) {
        this.publicGifItemBean = maps;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
        return view == o;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        ImageView imageView = new ImageView(context);
        imageView.setImageResource(R.mipmap.ic_empty);

        RecyclerView recyclerView = new RecyclerView(context);
        GridLayoutManager layoutManager = new GridLayoutManager(context, 4, GridLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        GiftAdapter adapter = new GiftAdapter(publicGifItemBean);
        adapter.setList(publicGifItemBean.get(position));
        recyclerView.setAdapter(adapter);

        if (YCollectionUtil.isNotEmpty(publicGifItemBean.get(position))) {
            container.addView(recyclerView);
            return recyclerView;
        } else {
            container.addView(imageView);
            return imageView;
        }
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }

}
